package hz.mobileproject.pda.ui.activity.work;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.codbking.widget.DatePickDialog;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.request.Request_data;
import hz.mobileproject.pda.entity.request.Request_rollCall_queryRcLog;
import hz.mobileproject.pda.entity.response.Response_CallRecord;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.ui.adapter.CallRecordAdapter;
import hz.mobileproject.pda.utils.Base64;

public class CallRecordActivity extends Activity implements View.OnClickListener {
    private Button _datePickBtn;
    private ListView _listView;

    private SPUtils mSPUtils;
    private Gson mGson;
    private CallRecordAdapter crAdapter;
    //机构号
    private String cusNumber;
    //部门id
    private String queryDept;
    //当前选择时间
    private String queryTime = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_record);
        initView();
        initData();
        initListener();
    }


    private void initView() {
        _datePickBtn = (Button) findViewById(R.id.bt_select_time_all);
        _listView = (ListView) findViewById(R.id.lv_call_record);
    }

    private void initData() {
        mSPUtils = SPUtils.getInstance("app_login");
        mGson = new Gson();
        crAdapter = new CallRecordAdapter(this,new ArrayList<Response_CallRecord>());
        cusNumber = mSPUtils.getString("cusnumber");
        queryDept = mSPUtils.getString("department_id");
        queryTime = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd"));
        //测试数据
//        queryDept = "65";
        //=============
        getCallRecordList();
    }

    private void initListener() {
        _datePickBtn.setText(queryTime);
        _datePickBtn.setOnClickListener(this);
        _listView.setAdapter(crAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_select_time_all://选择时间
                DatePickDialog dialog = new DatePickDialog(this);
                //设置上下年分限制
                dialog.setYearLimt(5);
                //回显当前选择时间
                try {
                    dialog.setStartDate(new SimpleDateFormat("yyyy-MM-dd").parse(queryTime));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //设置标题
                dialog.setTitle("选择时间");
                //设置类型
                dialog.setType(DateType.TYPE_YMD);
                //设置消息体的显示格式，日期格式
                dialog.setMessageFormat("yyyy-MM-dd HH:mm");
                //设置选择回调
                dialog.setOnChangeLisener(null);
                //设置点击确定按钮回调
                dialog.setOnSureLisener(new OnSureLisener() {
                    @Override
                    public void onSure(Date date) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        queryTime = sdf.format(date);
                        _datePickBtn.setText(queryTime);
                        LogUtils.e(queryTime);
                        //点击选择时间后 重新请求网络
                        getCallRecordList();
                    }
                });
                dialog.show();
                break;
        }
    }

    /**
     * 获取记录列表
     */
    private void getCallRecordList(){
        Request_rollCall_queryRcLog rcqr =
                new Request_rollCall_queryRcLog(cusNumber,queryDept,queryTime);

        String fpStr = mGson.toJson(rcqr);
        LogUtils.e(fpStr);
        Request_data data = new Request_data();
        data.setData(Base64.getBase64(fpStr));
        String body_data = mGson.toJson(data);

        RequestParams params = new RequestParams(Constance.BASE_URL + "/" + Constance.PROJECT_NAME +  "/rollCall/queryRcLog");
        params.setBodyContent(body_data);
        params.setHeader("token",SPUtils.getInstance("app_login").getString("token"));
        x.http().post(params, callback);
    }

    /**
     * 获取点名记录回调
     */
    private Callback.CommonCallback<String> callback = new Callback.CommonCallback<String>() {
        @Override
        public void onSuccess(String result) {
            try{
                JsonObject returnData = new JsonParser().parse(result).getAsJsonObject();
                String returnStr = Base64.getFromBase64(returnData.get("data").getAsString());
                LogUtils.e(returnStr);
                ArrayList<Response_CallRecord> listData = mGson.fromJson(
                        returnStr,new TypeToken<ArrayList<Response_CallRecord>>() {}.getType());
                crAdapter.setListData(listData);

            }catch (Exception e){
                e.printStackTrace();
            }
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
            LogUtils.e(ex.toString());
            Toast.makeText(CallRecordActivity.this,"获取数据失败",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCancelled(CancelledException cex) {

        }

        @Override
        public void onFinished() {

        }
    };

}
